<template>
	<div>
		<div>
			<div class="cu-bar bg-white margin-top solid-bottom">
				<div class="action">
					<text class="cuIcon-titles text-orange"></text>
				</div>
			</div>

		</div>
		<view class="container">
			<view class="title">
				<text>健康助手系统</text>
			</view>
			<view class="desc">
				<text>用户统一登录界面</text>
			</view>

			<view class="login-form">
				<input type="text" value="" placeholder="请输入用户名" v-model="username" />
				<input type="text" value="" placeholder="请输入密码" password="true" v-model="password" />
				<div class="buttonGroup">
					<button type="primary" @click="signIn">登录</button>
				</div>
				<navigator url="/pages/user/user_register" class="linkBtn" open-type="redirect">
					<text >没有账号？前往注册</text>
				</navigator>
			</view>
			admin:123456
		</view>
	</div>
</template>

<script>
	import WucTab from '@/components/wuc-tab/wuc-tab.vue';
	import uniIcons from "@/components/uni-icons/uni-icons.vue"
	export default {
		data() {
			return {
				username: '',
				password: '',
			}
		},
		components: {
			WucTab,
			uniIcons,
		},
		computed: {},
		onLoad() {
		},
		methods: {
			signIn() {
				const {
					username,
					password,
				} = this
				
				if (username.length < 3) {
					uni.showModal({
						content: '用户名长度不能小于3',
						showCancel: false
					})
					return
				}
				
				if (password.length < 6) {
					uni.showModal({
						content: '密码长度不能小于6',
						showCancel: false
					})
					return
				}
				uni.showLoading({
					title: '登录中...'
				})
				uniCloud.callFunction({
					name: 'ha_check_account',
					data: {
						username,
						password,
					},
				}).then((res) => {
					console.log(res);
					uni.hideLoading()
					if (res.result.status !== 0) {
						return Promise.reject(new Error(res.result.msg))
					}
					uni.setStorageSync('token', res.result.token)
					uni.setStorageSync('uid', res.result.uid)
					uni.showModal({
						content: '登录成功',
						showCancel: false
					})
					
					// if(!res.result.class_id){
					// 	if(this.userType==0){
					// 		uni.navigateTo({
					// 			url: '/pages/teacher_bind/teacher_bind'
					// 		});
					// 	}else if(this.userType==1 || this.userType==2){
					// 		uni.navigateTo({
					// 			url:'/pages/student_bind/student_bind'
					// 		})
					// 	}
					// }else{
					// 	uni.setStorageSync("class_id",res.result.class_id)
						
					// 	if(this.userType==1 || this.userType==2){
					// 		uni.setStorageSync("stu_no",res.result.stu_no)
					// 		uni.setStorageSync("stu_name",res.result.stu_name)
					// 	}
						
						uni.navigateTo({
							url: '/pages/index/index'
						});
					// }
					
				}).catch((err) => {
					console.log(err);
					uni.hideLoading()
					uni.showModal({
						content: '登录失败，' + err.message,
						showCancel: false
					})
				})
			},
			loginMp() {
				uni.showLoading({
					title: '登录中...'
				})
				
				this.getCode().then((code) => {
					console.log('code', code);
					const { userType } = this;
					return uniCloud.callFunction({
						name: 'login', // TODO
						data: {
							code,
							userType,
						}
					})
				}).then((res) => {
					uni.hideLoading()
					console.log(res);
					if (res.result.status !== 0) {
						return Promise.reject(new Error(res.result.msg))
					}
					uni.setStorageSync('token', res.result.token)
					uni.showModal({
						content: '登录成功',
						showCancel: false
					})
				}).catch((err) => {
					uni.hideLoading()
					uni.showModal({
						content: '出现错误，请稍后再试.' + err.message,
						showCancel: false
					})
				})
			},
			getCode() {
				return new Promise((resolve, reject) => {
					uni.login({
						provider: 'weixin',
						success(e) {
							if (e.code) {
								resolve(e.code)
							} else {
								reject(new Error('微信登录失败'))
							}
						},
						fail(e) {
							reject(new Error('微信登录失败'))
						}
					})
				})
			},
			validateToken() {
				uni.showLoading({
					title: '加载中...'
				});
				uniCloud.callFunction({
					name: 'validateToken',
					data: {
						token: uni.getStorageSync('token')
					}
				}).then((res) => {
					console.log(res);
					uni.hideLoading()
					uni.showModal({
						content: res.result.msg,
						showCancel: false
					})
				}).catch((err) => {
					uni.hideLoading()
					uni.showModal({
						content: '请求云函数发生错误，' + err.message,
						showCancel: false
					})
				})
			},
		}
	}
</script>

<style>
	.weixinBtn{ color: #007AFF; text-align: center; font-size: 22upx; border-top: 1upx #333333 solid; margin-top: 50upx; padding-top: 50upx;}
	.weixinBtn div{ color: #333333; margin-bottom: 20upx;}
	.linkBtn{ color: #007AFF; text-align: right; font-size: 22upx;}
	.swiper{ height: 100upx;}
	.swiper-title{ font-size: 30upx;}
	.buttonGroup{ display: flex;}
	.buttonGroup navigator{margin-right: 20upx; flex: 1;}
	.container {
		padding: 30px;
	}

	.title {
		text-align: center;
		font-size: 20px;
		font-weight: bold;
		padding: 20px 0px;
	}

	.desc {
		padding: 10px 0px;
		font-size: 14px;
		line-height: 22px;
		color: #999999;
	}

	.login-form input {
		height: 40px;
		border: solid 1px #DDDDDD;
		text-indent: 10px;
	}

	.login-form input,
	.login-form button {
		width: 100%;
		margin-bottom: 10px;
	}
</style>
